import {defineStore} from "pinia";
import {ElMessage} from "element-plus";
import {addTypes, delTypes, findAllTypes, findAllTypesList, getTypes, updateTypes} from "@/api/types";

export const TypesStore = defineStore('types', {
    state:()=>{
        return {
            // 列表
            typesList: [],
            // 详细信息
            typesInfo: {},
            // 类型所有数据
            typesAllList:[],
        }
    },
    actions:{
        async getTypeAllList() {
            const res = await findAllTypesList()
            this.typesAllList = res.result
        },
        addTypes(){
            return new Promise((resolve, reject)=>{
                addTypes(this.typesInfo).then(res => {
                    resolve(res.result)
                    this.typesInfo = {}
                }).catch(err => {
                    reject()
                })
            })
        },
        async getTypesList(query) {
            return new Promise((resolve, reject)=>{
                findAllTypes(query).then(res => {
                    this.typesList = res.result.list
                    resolve(res.result)
                }).catch(err => {
                    reject()
                })
            })
        },
        async getTypesInfo(id) {
            const {result} = await getTypes(id)
            this.typesInfo = result
            this.typesInfo.types_price = parseFloat(this.typesInfo.types_price)
        },
        updateTypes(id,data) {
            return new Promise(async (resolve, reject) => {
                updateTypes(id,data).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        async deleteTypes(id) {
            const {code} = await delTypes(id)
        },
    }
})